package datasource;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DynamicRoutingDataSource {

    /**
     * 所有数据库
     */
    private final Map<String, DataSource> dataSourceMap = new ConcurrentHashMap<>();


    DataSource getDataSource(String key){
        return dataSourceMap.get(key);
    }

    public synchronized DataSource  addDataSource(String ds, DataSource dataSource) {
        //增加新数据源
        DataSource oldDataSource = dataSourceMap.put(ds, dataSource);

        return oldDataSource;
    }

    public synchronized void removeDataSource(String key) {
        DataSource dataSource = dataSourceMap.remove(key);
        closeDataSource(key, dataSource);
    }

    private void closeDataSource(String key, DataSource dataSource) {

    }

}
